<?php
namespace data\admin;
use \data\BaseData;
/**
 * 登录日志
 */
class LoginLogsData extends BaseData{
    
    
    /**
     * 添加登录日志
     */
    public function add($data){        
        $check = [
            'userid'    => ['check'=>'must','msg'=> '登录用户ID','reg'=>'int'],
            'status'    => ['reg'=> [1,2]],
            'ip'        => '',
            'area'      => '',
            'useragent' => '',
            'addtime'   => ['val'=> SYS_TIME],
        ];
        $save = $this->verifyData($check,$data);
        return M('admin_logs_login')->insertGetId($save);
    }    
    
    /**
     * 读取用户登录次数
     */
    public function getLastLoginList($param){
        $where['userid'] = array("in",$param['userid']);
        $field = "count(userid) as logins,userid";
        $list = M("admin_logs_login")->field($field)->where($where)->group("userid")->select();
        return $list;
    }
    
    /**
     * 读取用户登录次数
     */
    public function getLastLogin($userid){
        $where['userid'] = $userid;
        $list = M("admin_logs_login")->where($where)->order('id desc')->find();
        return $list;
    }
    
    /**
     * 读取列表
     */
    public function getList($param){        
        $page = $param['page']?:1;
        $limit= $param['limit']?:20;
        $check = ['userid','status'];
        $where = $this->parseWhere($check,$param);
        $list  = M('admin_logs_login')->where($where)->limit($limit)->page($page)->order('id desc')->select();
        $total = M('admin_logs_login')->where($where)->count();
        return [            
            'page'  => $page,
            'limit' => $limit,
            'total' => $total,
            'list'  => $list,
        ];
    }
}